package com.ckcool.blog.admin.service;

import com.ckcool.blog.admin.pojo.Admin;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author ckcool
 */
@Component
public class SecurityUserService implements UserDetailsService {

    @Resource
    private AdminService adminService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Admin admin = adminService.findAdminByUsername(s);
        if(admin == null) {
            return null;
        }
        UserDetails userDetails = new User(s, admin.getPassword(), new ArrayList<>());
        return userDetails;
    }
}
